package org.brisling.satisfaction.queryService;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.satisfaction.domain.SatisfactionEvaluateResult;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController 
@RequestMapping("/satisfactionEvaluateResult/query")

public class QuerySatisfactionEvaluateResultService extends AbstractBaseQueryService<SatisfactionEvaluateResult>{
	
	
	@Autowired
	private QueryDictionaryService dicService;
	
	public QuerySatisfactionEvaluateResultService(){
		//test
		this.moduleName = ConstSysParam.MODULE_SATISFACTION_EVALUATE_RESULT;
		this.subSystem = ConstSysParam.SUBSYSTEM_STATISFACTION;
		this.tableName = this.moduleName;
		this.clazz = SatisfactionEvaluateResult.class;	
		orderMap.put("evaluateat","desc");	
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}

	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	@RequestMapping("/getListDomainByDeptid.action")
	public ListDomain<SatisfactionEvaluateResult> getListDomainByDeptid(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		if(deptid==null){
			ListDomain<SatisfactionEvaluateResult> listDomain = new ListDomain<SatisfactionEvaluateResult>();
			listDomain.setTotal(0l);
			listDomain.setRows(new ArrayList<SatisfactionEvaluateResult>());
			return listDomain;
		}
		
		String sql = dicService.getSqlByBizcode("user_jobrole_list");
		
		String whereCorse=String.format(("where deptid = %1$d"),deptid);
		sql = String.format(sql, whereCorse);		
		
		return super.getPagedListDomain(page,rows,sql);
	}
	
	
	
	@RequestMapping("/getList.action")
	public List<SatisfactionEvaluateResult> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public SatisfactionEvaluateResult getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOneByIdentify("id", id.toString());		
	}
	
	/**
	 * 根据唯一编码查找记录
	 * @param identify	记录唯一编码
	 * @return 信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public SatisfactionEvaluateResult getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	/**
	 * 根据部门id查找用户集合
	 * @param page	页码
	 * @param rows	行数
	 * @param deptid	部门编码
	 * @return	用户数据集
	 */
	@RequestMapping("/getUserByDeptid.action")
	public ListDomain<SatisfactionEvaluateResult> getSatisfactionEvaluateResultByDept(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Long deptid){
		
		String view_department = ConstSysParam.VIEW_PREFIX + ConstSysParam.MODULE_DEPARTMENT;
		String sql = " SELECT u.* FROM %1$s u LEFT JOIN %2$s d ON u.deptid=d.deptid AND d.deptid=%3$d" ;
		sql= String.format(sql, ConstSysParam.VIEW_USER, view_department,deptid);		
		
		return super.getPagedListDomain(page, rows, sql);
	}
	
	/**
	 * 根据指定字段及字段值查找记录集合
	 * @param field	字段名称
	 * @param val	字段值
	 * @return 记录集合
	 */
	@RequestMapping("/getListByIdentify.action")
	public List<SatisfactionEvaluateResult> getListByIdentify(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="val",required=false) String val){	
		return super.getListByIdentify(field,val);
	}

}
